﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace StudentManage
{
     class XL_BANG
    {
        private static string connection = "Server=10.9.9.20;Database=studentdb;User Id=phipp; Password=phippp;";
        static SqlConnection Con = new SqlConnection(connection);

        public static string XL_Ngay(DateTimePicker a)
        {
            return a.Value.Year + "/" + a.Value.Month + "/" + a.Value.Day;
        }


        public static DataTable Doc(String sql="")
        {
            Open();
            DataTable rs = new DataTable();
            SqlCommand com = new SqlCommand(sql, Con);
            SqlDataAdapter adapter = new SqlDataAdapter(com);
            adapter.Fill(rs);
            return rs;
        }

        public static int Update(string sql)
        {
            Open();

            SqlCommand com = new SqlCommand(sql, Con);
            return com.ExecuteNonQuery();

        }
        public static void Xuatngay(DataTable tb, ComboBox comb)
        {
            comb.DataSource = tb;
            comb.ValueMember = "id";
            comb.DisplayMember = "termname";
        }
        public static void Xuat(DataTable tb, ComboBox comb )
        {
            comb.DataSource = tb;
            comb.ValueMember = "id";
            comb.DisplayMember = "name";
        }
        public static void Xuathocky(DataTable tb, ComboBox comb)
        {
            comb.DataSource = tb;
            comb.ValueMember = "id";
            comb.DisplayMember = "name";
        }

        public static bool Run(string sql)
        {
            try
            {
                Open();
                SqlCommand com = new SqlCommand(sql, Con);
                com.ExecuteNonQuery();
                return true;
            }


            catch
            {
                return false;
            }
        }

         static void Open()
         {
             if (Con.State != ConnectionState.Open)
             {
                 Con.Open();
             }
         }

         static void Close()
         {
             if (Con.State == ConnectionState.Open)
             {
                 Con.Close();
             }
         }
    }
}
